class Singleton{
    private static Singleton instance=null;
    private static Object locker;
    public static Singleton getInstance() {
        if(instance==null) {
            synchronized (locker) {
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
    //构造方法私有，在类外部不允许new新的实例
    private Singleton(){}

}

//class Singleton{
//    //在这个类被加载的时候，初始化这个静态成员
//    private static Singleton instance=new Singleton();
//
//    public static Singleton getInstance() {
//        return instance;
//    }
//    //构造方法私有，在类外部不允许new新的实例
//    private Singleton(){}
//}

public class ThreadDemo2 {
    public static void main(String[] args) {
        Singleton s1=Singleton.getInstance();
        Singleton s2=Singleton.getInstance();
        System.out.println(s1==s2);
    }

}
